package com.woniu.demo.controller;

import com.github.pagehelper.PageInfo;
import com.woniu.demo.pojo.Standard;
import com.woniu.demo.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.*;
import java.sql.Statement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @Description TODO
 * @date 2019/9/20
 */
@Controller
public class StandardController {

    @Value("${filepath}")
    private String filePath;

    @Autowired
    private StandardService standardService;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "currentPage",required = false,defaultValue = "1") Integer currentPage,
                             @RequestParam(value = "pageSize",required = false,defaultValue = "5") Integer pageSize,
                             String condition){
        List<Standard> list = standardService.findStandards(currentPage,pageSize,condition);
        PageInfo<Standard> pageInfo = new PageInfo<>(list);
        ModelAndView modelAndView = new ModelAndView("list");
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.addObject("condition",condition);
        return modelAndView;
    }

    @DeleteMapping("/deleteStandard")
    @ResponseBody
    public Map delete(Integer[] keys){
        Map result = new HashMap();
        try {
            standardService.removeBatch(keys);
            result.put("code","0");
            result.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","500");
            result.put("msg","删除失败");
        }
        return result;
    }

    @PostMapping("/saveStandard")
    @ResponseBody
    public Map saveStandard(Standard standard,@RequestParam("file") MultipartFile file){
        Map result = new HashMap();
        try {
            standardService.addStandard(standard,file,filePath);
            result.put("code","0");
            result.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            result.put("code","500");
            result.put("msg","添加失败");
        }
        return result;
    }

    @RequestMapping("/down/{id}")
    @ResponseBody
    public ResponseEntity<byte[]> down(@PathVariable("id")Integer id){
        Standard standard = standardService.findStandardById(id);
        String path = standard.getPackagepath();
        try {
            InputStream inputStream = new FileInputStream(new File(path));
            byte[] body = new byte[inputStream.available()];//available();获取字节流中可读取的数据长度
            inputStream.read(body);
            HttpHeaders headers = new HttpHeaders();
            //以附件形式返回
            String fname = standard.getPackagepath().substring(standard.getPackagepath().lastIndexOf("\\")+1);
            headers.setContentDispositionFormData("attachment",fname);
            ResponseEntity<byte[]> entity = new ResponseEntity<>(body,headers, HttpStatus.OK);
            return entity;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
